﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZhonTai.Common.Helpers
{
    public static class DataTableHelper
    {
        public static List<Dictionary<string, object>> ToDictionaryList(this DataTable dt)
        {
            var list = new List<Dictionary<string, object>>();
            if (dt == null || dt.Rows.Count == 0) return list;

            foreach (DataRow row in dt.Rows)
            {
                var dict = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    var value = row[col];
                    // 替换 DBNull 和空值为字符串
                    dict[col.ColumnName] = (value == DBNull.Value || value == null) ? string.Empty : value;
                }
                list.Add(dict);
            }
            return list;
        }
    }
}